package com.pshcp.patient.tool;

import cn.hutool.core.date.DateUtil;
import com.external.pay.domain.PayReqDTO;
import com.framework.thread.ReqComHold;
import com.framework.tool.CommonTool;
import com.framework.util.SystemUtil;
import com.pshcp.common.enums.CommonYesNoEnum;
import com.pshcp.common.enums.FeedbackTypeEnum;
import com.pshcp.common.enums.OnlineBizStatusEnum;
import com.pshcp.common.enums.PayBusinessEnum;
import com.pshcp.common.enums.RegPayStatusEnum;
import com.pshcp.common.enums.UserTypeEnum;
import com.pshcp.domain.dto.PatientOnlineOrderDTO;
import com.pshcp.domain.model.Feedback;
import com.pshcp.domain.model.OnlineSession;
import com.pshcp.domain.model.PatientOnlineOrder;
import com.pshcp.patient.domain.onlineorder.ao.OnlineConfirmAO;
import com.pshcp.patient.domain.onlineorder.ao.OnlineListAO;
import com.pshcp.patient.domain.onlineorder.ao.OnlineSuggestAO;
import com.pshcp.patient.domain.onlineorder.ao.OnlineToPayAO;

import java.util.Date;

public class OnlineOrderTools {

    /**
     * 构建在线咨询单
     * @param confirmAO
     * @return
     */
    public static PatientOnlineOrder buildConfirmAO2OnlineOrder(OnlineConfirmAO confirmAO){
        PatientOnlineOrder patientOnlineOrder = new PatientOnlineOrder();
        patientOnlineOrder.setDoctorId(confirmAO.getDoctorId());
        patientOnlineOrder.setPatientId(confirmAO.getPatientId());
        patientOnlineOrder.setPatientUserId(ReqComHold.getReqCom().getUserId());
        patientOnlineOrder.setBizStatus(OnlineBizStatusEnum.START.getCode());
        patientOnlineOrder.setPrescriptionStatus(CommonYesNoEnum.NO.getCode());
        patientOnlineOrder.setPayStatus(RegPayStatusEnum.WAIT_PAY.getCode());
        return patientOnlineOrder;
    }

    public static PatientOnlineOrderDTO buildListAO2DTO(OnlineListAO onlineListAO){
        PatientOnlineOrderDTO orderDTO = new PatientOnlineOrderDTO();
        orderDTO.setPatientUserId(ReqComHold.getReqCom().getUserId());
        orderDTO.setPageNum(onlineListAO.getPageNum());
        orderDTO.setPageSize(onlineListAO.getPageSize());
        if(onlineListAO.getLastDay() != null){
            orderDTO.setEndTime(new Date());
            orderDTO.setStartTime(DateUtil.offsetDay(orderDTO.getEndTime(), -onlineListAO.getLastDay()));
        }
        return orderDTO;
    }

    public static OnlineSession buildOnlineSession(PatientOnlineOrder onlineOrder){
        OnlineSession onlineSession = CommonTool.copyProperties(onlineOrder, OnlineSession.class);
        onlineSession.setId(SystemUtil.getId());
        onlineSession.setType(0);
        onlineSession.setPatientOnlineOrderId(onlineOrder.getId());
        onlineSession.setBeginTime(System.currentTimeMillis());
        onlineSession.setSessionStatus(OnlineBizStatusEnum.START.getCode());
        onlineSession.setPatientUnReadCount(0L);
        onlineSession.setDoctorUnReadCount(0L);
        onlineSession.setPatientDelStatus(0);
        onlineSession.setDoctorDelStatus(0);
        return onlineSession;
    }

    public static PayReqDTO buildPayDTO(PatientOnlineOrder onlineOrder, OnlineToPayAO toPayAO){
        PayReqDTO payReqDTO = new PayReqDTO();
        payReqDTO.setPayMode(toPayAO.getPayMode());
        payReqDTO.setWechatType(toPayAO.getWechatType());
        payReqDTO.setOpenid(toPayAO.getOpenid());
        payReqDTO.setOrderId(onlineOrder.getId());
        payReqDTO.setPayBusiness(PayBusinessEnum.ONLINE_PAY.getCode());
        payReqDTO.setAmount(onlineOrder.getPayFee());
        payReqDTO.setPatientUserId(onlineOrder.getPatientUserId());
        return payReqDTO;
    }

    public static Feedback buildOnlineSuggest(OnlineSuggestAO suggestAO){
        Feedback feedback = CommonTool.copyProperties(suggestAO, Feedback.class);
        feedback.setUserType(UserTypeEnum.PATIENT.getCode());
        feedback.setBizType(FeedbackTypeEnum.ONLINE_FB.getCode());
        feedback.setUserId(ReqComHold.getReqCom().getUserId());
        return feedback;
    }

}
